﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NetCoreCMS.Common;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;
namespace NetCoreCMS.Web.Areas.Admin.Controllers
{
    public class Manager_PwdController : ManageController
    {
        private const string WEB_VIEW = "~/Areas/Admin/Views/Manager/Manager_Pwd.cshtml";
        //
        // GET: /admin/Manager_Pwd/

        public ActionResult Index()
        {
            var model = GetAdminInfo();
            ShowInfo(model.id);
            return View(WEB_VIEW);
        }

        [HttpPost]
        public ActionResult SubmitSave()
        {
            ActionResult result = View(EDIT_RESULT_VIEW);
            var model = GetAdminInfo();

            if (AESEncryptHelper.Encode(HttpContext.Request.Form["txtOldPassword"].ToString().Trim(), model.salt) != model.password)
            {
                ViewBag.ClientScript = JscriptMsg("旧密码不正确！", "back");
                return result;
            }
            if (HttpContext.Request.Form["txtPassword"].ToString().Trim() != HttpContext.Request.Form["txtPassword1"].ToString().Trim())
            {
                ViewBag.ClientScript = JscriptMsg("两次密码不一致！", "back");
                return result;
            }
            model.password = AESEncryptHelper.Encode(Request.Form["txtPassword"].ToString().Trim(), model.salt);
            model.real_name = HttpContext.Request.Form["txtRealName"].ToString().Trim();
            model.telephone = HttpContext.Request.Form["txtTelephone"].ToString().Trim();
            model.email = HttpContext.Request.Form["txtEmail"].ToString().Trim();

            if (!_manager.Update(model))
            {
                ViewBag.ClientScript = JscriptMsg("保存过程中发生错误！", "back");
                return result;
            }
            HttpContext.Session.Remove(CMSKeys.SESSION_ADMIN_INFO);
            ViewBag.ClientScript = JscriptMsg("密码修改成功！", "index");
            return result;
        }

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            var model = _manager.GetModel(_id);
            ViewData["model"] = model;
        }
        #endregion
    }
}
